package com.example.esp8266.activity.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.esp8266.R;
import com.example.esp8266.activity.bean.Product;

import java.util.List;

/**
 * @Author lgd
 * @Date 2024/4/30 13:42
 *   网格 布局
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Product> list;
    private Context context;


    public MyAdapter(List<Product> list, Context context) {
        this.list = list;
        this.context = context;
    }

    //创建 ViewHolder:放recyclerview_item.xml 页面组件用的
    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //1 获取recyclerview_item.xml页面
        View view = View.inflate(context, R.layout.recyclerview_item,null);
        return new MyViewHolder(view);
    }

    //3 textView里面放置bean的数据
    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        holder.textView.setText(list.get(position).getProductName());
    }

    //recyclerview里面textView里面放了多少条数据
    @Override
    public int getItemCount() {
        //textView里面的list为空时返回0,不为空时,返回list实际的大小
        return list==null ? 0: list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        //2 获取textView
        private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.itemV);
            //设置自定义监听
            //当点击itemView时,触发onClick方法
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(monRecyclerItemClickListener!=null){
                        //调用自定义接口的方法
                        monRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
            // 长按  设置 长按 时间
//            LongClickUtils.setLongClick(new Handler(), itemView, 1000, new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View view) {
//                    if (monLongRecyclerItemClickListener != null){
//                        monLongRecyclerItemClickListener.onLongRecyclerItemClick(getAdapterPosition());
//                    }
//                    return true;
//                }
//            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (monLongRecyclerItemClickListener != null){
                        monLongRecyclerItemClickListener.onLongRecyclerItemClick(getAdapterPosition());
                    }
                    return true;
                }
            });
        }
    }
    //自定义监听
    //自定义接口的对象   点击事件
    private onRecyclerItemClickListener monRecyclerItemClickListener;

    public void setMonRecyclerItemClickListener(onRecyclerItemClickListener listener) {
        monRecyclerItemClickListener = listener;
    }
    public interface onRecyclerItemClickListener{//自定义一个接口
        void onRecyclerItemClick(int position);
    }

    /* 长按 事件 */
    private onLongRecyclerItemClickListener monLongRecyclerItemClickListener;
    public void setLongMonRecyclerItemClickListener(onLongRecyclerItemClickListener listener) {
        monLongRecyclerItemClickListener = listener;
    }
    public interface onLongRecyclerItemClickListener{
        void onLongRecyclerItemClick(int position);
    }


}

